import sys
from PyQt5.QtWidgets import QApplication, QWidget, QStackedLayout, QLabel, QVBoxLayout, QPushButton


class Widget1(QWidget):
    def __init__(self):
        super().__init__()
        QLabel('Here is Widget1', self).resize(600, 100)


class Widget2(QWidget):
    def __init__(self):
        super().__init__()
        QLabel('Here is Widget2', self).resize(600, 100)


class MyWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.create_stacked_layout()
        self.init_ui()

    def init_ui(self):
        self.resize(700, 600)
        layout = QVBoxLayout()

        btn1 = QPushButton('switch to Window1')
        btn2 = QPushButton('switch to Window2')
        # 为按钮添加事件
        btn1.clicked.connect(self.btn1_clicked)
        btn2.clicked.connect(self.btn2_clicked)

        widget = QWidget()
        widget.setLayout(self.stack_layout)

        layout.addWidget(widget)
        layout.addWidget(btn1)
        layout.addWidget(btn2)

        self.setLayout(layout)

    def create_stacked_layout(self):
        self.stack_layout = QStackedLayout()

        widget1 = Widget1()
        widget2 = Widget2()
        self.stack_layout.addWidget(widget1)
        self.stack_layout.addWidget(widget2)

    def btn1_clicked(self):
        self.stack_layout.setCurrentIndex(0)

    def btn2_clicked(self):
        self.stack_layout.setCurrentIndex(1)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    my_window = MyWindow()
    my_window.show()
    app.exec_()
